import { RCIMIWEngine,RCIMIWEngineOptions,RCIMIWConversationType,RCIMIWMessage } from "@rongcloud/react-native-im-wrapper";


engine.setOnConnectedListener((code:number,userId:string)=>{
    console.log(code,userId);
});

engine.connect("5Oj+tq9hSzvVjl4uKPnqU0+vXbeemLms@9hgq.cn.rongnav.com;9hgq.cn.rongcfg.com",5000);

engine.setOnMessageReceivedListener((message,left,offline,hasPackage)=>{
    console.log(message,left,offline,hasPackage);
});



interface Callback {
    (code:number,userId:string):void
}

interface ReceivedMessageCallback {
    (message:RCIMIWMessage,left:number,offline:boolean,hasPackage:boolean):void
}

export default class RCloudEngine {
    private _engine:RCIMIWEngine;
    private static _instance;
    static get Ins(){
        if(!RCloudEngine._instance){
            RCloudEngine._instance = new this();
        }
        return RCloudEngine._instance;
    }


    constructor(){
        this._engine = RCIMIWEngine.create("p5tvi9dspk294",{});

    }

    connect(token:string,timeout:number,statusChange:Callback){
        this._engine.setOnConnectedListener(statusChange);
        this._engine.connect(token,timeout);
    }

    addMessageReceivedListener(fn:ReceivedMessageCallback){
        this._engine.setOnMessageReceivedListener(fn);
    }

    /**
     * 断开连接
     * @param receivePush 退出后是否接受push
     * @returns 当次接口操作状态码
     */
    disconnect(receivePush:boolean):Promise<number>{
        return this._engine.disconnect(receivePush);
    }

    /**
     * 发送文本消息
     * @param textMessage 消息对象
     * @returns 当次接口操作状态码
     */
    async sendTextMessage(textMessage){
        const { targetId,channelId,text } = textMessage;
        const message = await  this._engine.createTextMessage(RCIMIWConversationType.PRIVATE,targetId,channelId,text);
        const code = await this._engine.sendMessage(message);
        return code;
    }

    /**
     * 获取会话列表
     * @param startTime 会话开始时间
     * @param count 会话数量
     */
    loadConversations(startTime:number,count:number){
        this._engine.loadConversations([RCIMIWConversationType.PRIVATE,RCIMIWConversationType.SYSTEM,RCIMIWConversationType.GROUP],"",startTime,count);
        this._engine.setOnConversationsLoadedListener;

    }


   
}